package com.gxbsanyuan.alarmalapp;
import java.util.HashMap;
import java.util.Map;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;


public class CustomPushReceiver extends MessageReceiver {
    private static final int NOTIFICATION_ID = 1001;
    private static final int REQUEST_CODE = 1002;
    private static final String REC_TAG = "MPS:CustomPushReceiver";

    // 推送消息到达时触发
    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        android.util.Log.e(REC_TAG, "Received message: " + cPushMessage.getContent());
        scheduleReminder(context);
    }

    // 推送通知到达时触发
    @Override
    public void onNotification(Context context, String title, String summary, java.util.Map<String, String> extraMap) {
        android.util.Log.e(REC_TAG, "Received notification: " + title + " - " + summary);
        scheduleReminder(context);
    }
    @Override
    public void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {

    }
    @Override
    public void onNotificationRemoved(Context context, String messageId) {

    }
    @Override
    public void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {


    }

    @Override
    public void onNotificationOpened(Context context, String title, String summary,
                                     String extraMap) {

    }
    private void scheduleReminder(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = null;
            notification = new Notification.Builder(context, "alarm")
                    .setContentTitle("新消息提醒")
                    .setContentText("您有一条未读消息，请查看！")
                    .setSmallIcon(android.R.drawable.ic_dialog_info)
                    .setAutoCancel(false)
                    .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                    .setContentIntent(getPendingIntent(context))
                    .build();


        notificationManager.notify(NOTIFICATION_ID, notification);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(context, ReminderReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        long interval = 60 * 1000; // 60秒间隔
        long triggerTime = System.currentTimeMillis() + interval;
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, interval, pendingIntent);

        }
    }

    private PendingIntent getPendingIntent(Context context) {
        Intent intent = new Intent(context, com.gxbsanyuan.alarmalapp.MainActivity.class);
        intent.putExtra("notification_id", NOTIFICATION_ID);
        return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public static void stopReminder(Context context) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, ReminderReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.cancel(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_ID);
    }
}